package com.example.serverbinder

import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log

class ServerService : Service() {
    private val TAG = "====="
    private val mList = mutableListOf<User>()
    override fun onCreate() {
        super.onCreate()
        Log.i(TAG, "onCreate:1 ")
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.i(TAG, "onStartCommand: ")
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onBind(intent: Intent): IBinder {
        Log.i(TAG, "onBind: ")
        return ServerBinder()
    }

    inner class ServerBinder : IController.Stub() {

        override fun add(txt: User?) {
            txt?.let {
                mList.add(it)
            }
        }

        override fun remove(index: Int) {
            if (index < 0 || index > mList.size) {
                return
            }
            mList.removeAt(index)
        }

        override fun getList(): MutableList<User> {
            return mList
        }
    }
}